var serverHost = "http://120.79.227.110";// 注意:更改main.js
var param = {};
AppContext = {
	
	/**
	 * 所有跳转使用这个Router对象实现
	 * 
	 * */
	Router: function () {
		this.clear = function () {
			param = {};
		}
		//每次跳转首先处理并存储接收传过来的参数
		this.dealParam = function (others) {
			var search = location.search;

			if (search) {
				search = search.replace("?", "");
				$.each(search.split("&"), function () {
					var sers = this.split("=");
					if (sers.length > 1) {
						if (sers[0] == "psids") {
							var user_psids = decodeURIComponent(sers[1]).split(",");
							param.psids = user_psids + '';
						} else {
							param[sers[0]] = decodeURIComponent(sers[1]);
						}
					}
				});
			}
			if(others){
				for(var key in others ){
					param[key] = others[key]+"";
				}
			}
		}
		this.href = function (url, data) {
			var oldurl = url;
			if (data) {
				for (var key in data) {
					param[key] = data[key];
				}
			}
			var urlparam = {}; 
			if (url.indexOf("?") > -1) {
				var search = url.split("?")[1];
				url = url.split("?")[0];
				if (search) {
					$.each(search.split("&"), function () {
						var sers = this.split("=");
						if (sers.length > 1) {
							if (sers[0] == "psids") {
								var user_psids = decodeURIComponent(sers[1]).split(",");
								param.psids = user_psids + '';
							} else {
								param[sers[0]] = decodeURIComponent(sers[1]);
							}
						}
					});
				}
			}
			var urlid = url.split("/").pop().split(".")[0];
			try{
				var  detailPage = plus.webview.getWebviewById(urlid);
				mui.fire(detailPage,'refresh',param);
			}catch(e){}
			try{if(closeSingleWebViews)closeSingleWebViews();}catch(e){}
			localStorage.setItem("data",JSON.stringify(param));
			mui.openWindow({
			   url: url,
			   id: urlid,
			   createNew: false,
			   show: {
				   	autoShow:true,
			      	aniShow:'none',
			   },
			   waiting: {
			   	autoShow: true
			   }
			})
			//location.href = url + "?" + $.param(param);
		}
		
	},
	}
	window.router = new AppContext.Router();
	router.dealParam();
	logSrv = {
			// 登录
			login: function () {
				var username = $("#username").val();
				var password = $("#password").val();
				$.ajax({
					type: "POST",
					url: serverHost + "/mobile/login",
					data: { username: username, password: password },
					dataType: 'json',
					success: function (data) {
						if (data.errorMsg) {
							mui.alert(data.errorMsg);
						} else {
							localStorage.setItem('username',username);
							localStorage.setItem('password',password);
							localStorage.setItem('logpassword',data.logpassword);
							delete data.logpassword;
							localStorage.setItem('data',JSON.stringify(data));
							router.dealParam(data);
							delete param.islog;
							router.href("./index.html");
						}
					},
					error:function(){
						mui.alert("登录失败!");
					}
				})
			},
			// 退出
			logout: function () {
				router.clear();
				location.href = "./login.html";
			}
	}
$(function(){

})

